package com.maybach.workflow.core.config.parser;


import com.maybach.util.AssertUtil;
import com.maybach.workflow.common.api.node.BaseNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

/**
 * Created by well on 15/6/12.
 */
public abstract  class AbstractParser<T extends BaseNode> implements Parser{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected String nodeName;

    public abstract T  createNode();
    public AbstractParser(String nodeName){
        this.nodeName = nodeName;
    }

    @Override
    public T parse(Element element) {
        if(element == null){
            return null;
        }
        if(!nodeName.equalsIgnoreCase(element.getNodeName())){
            return null;
        }
        T baseNode = createNode();

        baseNode.setName(element.getAttribute(NodeParser.ATTR_NAME));
        baseNode.setTo(element.getAttribute(NodeParser.ATTR_TO));
        baseNode.setDisplayName(element.getAttribute(NodeParser.ATTR_DISPLAYNAME));

        parseSpecial(element, baseNode);
        check(baseNode);
        return baseNode;
    }

    public abstract void parseSpecial(Element element, T specialNode);

    public void check(T baseNode){
        AssertUtil.notNull(baseNode, "baseNode");
        AssertUtil.notBlank(baseNode.getName(), "baseNode");
    }
}
